/***
 *  created by wangwei at 2014-3-25
 *  Response用户返回数据，包括状态，数据对象(List, Entity)
 */
package com.ch.sys.utils;

import java.util.HashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ch.fastbuilder.common.PageResult;
import com.ch.sys.common.Codes;

@SuppressWarnings("serial")
public class Response extends HashMap<String,Object> {
	
	private static String CODE = "code";
	private static String DATA = "data";
	private static String TOTAL = "total";
	private static String MESSAGE = "message";
	private Logger logger = LoggerFactory.getLogger(Response.class); 
	
	private Response() {
		this.put(CODE, Codes.SUCCESS);
	}
	
	public static Response newResponse() {
		return new Response();
	}
	
	public static Response set(String key, Object value) {
		Response response = Response.newResponse();
		response.put(key, value);
		return response;
	}
	
	public Response put(String key, Object value) {
		super.put(key, value);
		return this;
	}
	
	public void setData(Object data){
		this.put(DATA, data);
	}
	
	public Response moveTo(String fromKey, String toKey) {
		Object val = this.get(fromKey);
		this.put(toKey, val);
		this.remove(fromKey);
		return this;
	}
	
	public Boolean isOK() {
		return this.getCode() == Codes.SUCCESS;
	}
	
	public Boolean isFail() {
		return !isOK();
	}
	
	public Response ok(Object value) {
		super.put(DATA, value);
		return this;
	}
	
	public Response ok(String key, Object value) {
		super.put(key, value);
		return this;
	}
	
	public Response setResults(Integer total, Object rows) {
		this.setTotal(total);
		this.ok(rows);
		return this;
	}
	
	public Response setPageResults(PageResult pageResult) {
		this.setTotal(pageResult.getTotal());
		this.ok(pageResult.getData());
		return this;
	}
	
	public Response results(Integer total, Object rows) {
		this.setTotal(total);
		this.ok(rows);
		return this;
	}
	
	private Response setCode(int code) {
		this.put(CODE, code);
		return this;
	}
	
	private static Response code(int code) {
		Response result = new Response();
		result.setCode(code);
		
		return result;
	}
	
	public int getCode() {
		return Integer.parseInt(this.get(CODE).toString());
	}
	
	public Response error(Exception e) {
		e.printStackTrace();
		logger.error("ExceptionError",e);
		logger.error("Exception",e.toString()+e.getLocalizedMessage()+e.getMessage());
		
		Response response = Response.newResponse();
		return response.SERVER_ERROR();
	}
	
	public Response setTotal(Integer total) {
		this.put(TOTAL, total);
		return this;
	}
	
	public static Response set(Integer total, Object rows) {
		Response response = new Response();
		response.setTotal(total);
		response.ok(rows);
		
		return response;
	}
	
//============状态===================================================================================	
	
    public Response OK() {
		Response response = Response.code(Codes.SUCCESS);
		response.put(MESSAGE, "ok");
		return response;
	}
	
	public Response SERVER_ERROR() {
		Response response = Response.code(Codes.SERVER_ERROR);
		response.put(MESSAGE, "server error");
		return response;
	}
    
	public Response ACCOUNT_EXISTS() {
		Response response = Response.code(Codes.ACCOUNT_EXISTS);
		response.put(MESSAGE, "account exists");
		return response;
	}
	
	public Response ACCOUNT_LOCKED() {
		Response response = Response.code(Codes.ACCOUNT_LOCKED);
		response.put(MESSAGE, "account locked");
		return response;
	}
	
	public Response ACCOUNT_NOT_EXISTS() {
		Response response = Response.code(Codes.ACCOUNT_NOT_EXISTS);
		response.put(MESSAGE, "account not exists");
		return response;
	}
	
	public Response ACCOUNT_PASS_ERROR() {
		Response response = Response.code(Codes.ACCOUNT_PASS_ERROR);
		response.put(MESSAGE, "account password error");
		return response;
	}
	
	public Response PASSWORD_EMPTY() {
		Response response = Response.code(Codes.PASSWORD_EMPTY);
		response.put(MESSAGE, "password empty");
		return response;
	}
	
	public Response PASSWORD_ERROR() {
		Response response = Response.code(Codes.PASSWORD_ERROR);
		response.put(MESSAGE, "password error");
		return response;
	}
	
	public Response BALANCE_NOT_ENOUGH() {
		Response response = Response.code(Codes.BALANCE_NOT_ENOUGH);
		response.put(MESSAGE, "blance not enough");
		return response;
	}
	
	public Response POINT_NOT_ENOUGH() {
		Response response = Response.code(Codes.POINT_NOT_ENOUGH);
		response.put(MESSAGE, "point not enough");
		return response;
	}
	
	public Response EXCEL_ERROR() {
		Response response = Response.code(Codes.EXCEL_ERROR);
		response.put(MESSAGE, "excel error");
		return response;
	}
	
	public Response EXCEL_FORMAT() {
		Response response = Response.code(Codes.EXCEL_FORMAT);
		response.put(MESSAGE, "excel format");
		return response;
	}
	
	public Response MOBILE_EXISTS() {
		Response response = Response.code(Codes.MOBILE_EXISTS);
		response.put(MESSAGE, "mobile is exists!");
		return response;
	}
	
	public Response NOT_PERMISSION() {
		Response response = Response.code(Codes.NOT_PERMISSION);
		response.put(MESSAGE, "not permission!");
		return response;
	}
	
	public Response OVER_COUNT() {
		Response response = Response.code(Codes.OVER_COUNT);
		response.put(MESSAGE, "over count");
		return response;
	}
	
	public Response PARAM_ERROR() {
		Response response = Response.code(Codes.PARAM_ERROR);
		response.put(MESSAGE, "parameters is error!");
		return response;
	}
	
	public Response APPKEY_ERROR(){
		Response response = Response.code(Codes.APPKEY_ERROR);
		response.put(MESSAGE, "appKey and appSecret is error!");
		return response;
	}
	
	public Response APPKEY_EXPIRED(){
		Response response = Response.code(Codes.APPKEY_EXPIRED);
		response.put(MESSAGE, "appkey expired");
		return response;
	}
	
	public Response CODE_ERROR(){
		Response response = Response.code(Codes.CODE_ERROR);
		response.put(MESSAGE, "code is error!");
		return response;
	}

	public Response CODE_EXPIRED(){
		Response response = Response.code(Codes.CODE_EXPIRED);
		response.put(MESSAGE, "code expried!");
		return response;
	}
	
	public Response TOKEN_ERROR(){
		Response response = Response.code(Codes.TOKEN_ERROR);
		response.put(MESSAGE, "token error!");
		return response;
	}
	
	public Response TOKEN_EXPIRED(){
		Response response = Response.code(Codes.TOKEN_EXPIRED);
		response.put(MESSAGE, "token expried!");
		return response;
	}
	
	public Response COUPONS_EXPIRED(){
		Response response = Response.code(Codes.COUPONS_EXPIRED);
		response.put(MESSAGE, "coupons expired");
		return response;
	}
	
	public Response ACTIVITY_EXISTS_IN_DATE() {
		Response response = Response.code(Codes.ACTIVITY_EXISTS_IN_DATE);
		response.put(MESSAGE, "activity exists in date");
		return response;
	}
	
	public Response CANCEL_ORDER_INVALID_DATE() {
		Response response = Response.code(Codes.CANCEL_ORDER_INVALID_DATE);
		response.put(MESSAGE, "cancel order invalid date");
		return response;
	}
	
	public Response SMS_EXPIRED(){
		Response response = Response.code(Codes.SMS_EXPIRED);
		response.put(MESSAGE, "sms expired");
		return response;
	}
	
	public Response EXCEL_OVERFLOW(){
		Response response = Response.code(Codes.EXCEL_OVERFLOW);
		response.put(MESSAGE, "excel overflow");
		return response;
	}
	
	public Response XINGGE_BROADCAST_OUT_OF_BOUNDS(){
		Response response = Response.code(Codes.XINGGE_BROADCAST_OUT_OF_BOUNDS);
		response.put(MESSAGE, "当天广播次数已用完");
		return response;
	}
	
//==================================================================================================	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}